package com.javatiaocao.myblog.controller;

import com.javatiaocao.myblog.constant.CodeType;
import com.javatiaocao.myblog.model.Categories;
import com.javatiaocao.myblog.service.ArticleService;
import com.javatiaocao.myblog.service.CategoryService;
import com.javatiaocao.myblog.utils.DataMap;
import com.javatiaocao.myblog.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ArticleService articleService;

    //添加或删除分类
    @PostMapping("/updateCategory")
    public String updateCategory(Categories categories,int type){
        try {
            DataMap dataMap = categoryService.addCategory(categories,type);
            return JsonResult.build(dataMap).toJSON();
        }catch (Exception e){
            log.error("addCategory is error"+e);
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    //下拉列表分类（发表文章界面）
    @GetMapping("/findCategoriesName")
    public String findCategoriesName(){
        try {
            DataMap dataMap = categoryService.findCategoriesName();
            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("addCategory is error"+e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     *
     *
     * 目录页显示/findCategoriesNameAndArticleNum
     *
     * **/
    @GetMapping("/findCategoriesNameAndArticleNum")
    public String findCategoriesNameAndArticleNum(){
        try {
            DataMap dataMap = categoryService.findCategoriesNameAndNum();
            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("findCategoriesNameAndArticleNum is error"+e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     *
     *
     * /getCategoryArticle
     *
     * */
    @GetMapping("/getCategoryArticle")
    public String getCategoryArticle(Integer rows,Integer pageNum,String category){
        try {
            DataMap dataMap = categoryService.getCategoryArticle(rows,pageNum,category);
            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("getCategoryArticle is error"+e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }


}
